//
//  UIButton+Extension.swift
//  NSinaWeibo
//
//  Created by LoliQueen on 16/2/19.
//  Copyright © 2016年 LoliQueen. All rights reserved.
//

import UIKit


extension UIButton {

    //给 UIButton 扩展一个 构造函数
    //1.背景图片 + 图片
    convenience init(backImageName: String?,imageName: String?) {
        self.init()
        
        //按钮如果有背景图片，就会自动设置大小
        if backImageName != nil{
        setBackgroundImage(UIImage(named: backImageName!), forState: .Normal)
            setBackgroundImage(UIImage(named: backImageName! + "_highlighted"), forState: .Highlighted)
        }
        if imageName != nil {
            setImage(UIImage(named: imageName!), forState: .Normal)
            
            //设置高亮
            setImage(UIImage(named: imageName! + "_highlighted"), forState: .Highlighted)
        
        }
        
        sizeToFit() 
    }
    
    //2.背景图片 + 文案
    convenience init(backImageName: String?,title: String,textColor: UIColor,imageName: String? = nil,fontSize: CGFloat = 14) {
        self.init()
        if backImageName != nil {
            //按钮的大小会根据北京图片自动修改
            setBackgroundImage(UIImage(named: backImageName!), forState: .Normal)
        }
        if imageName != nil {
            setImage(UIImage(named: imageName!), forState: .Normal)
        }
        
        //设置字体的大小
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        setTitle(title, forState: .Normal)
        setTitleColor(textColor, forState: .Normal)
        //自适应大小
        sizeToFit()
    }
    
}